from Common import sql_content


class _Policy(object):
    def __init__(self,min,max):
        self.sql_content = sql_content.sql_content()
        self.min = min
        self.max = max
        self.dict_turn = {'EmploymentServices':'就业服务', 'InnovateAndServices':'创新创业', 'NewsCenter':'新闻中心', 'FAQs':'常见问题', 'SocialSecurity':'社会保险', 'Weekly':'周刊'}
        self.policy_all = []

    def policy_main(self):
        self.policy_sql()
        #self.policy_sql_type()
        return self.policy_all

    #连接Policy数据库
    def policy_sql(self):
        print(self.min,self.max)
        con = self.sql_content.conten()
        sql_policy = "select * from policy WHERE %d <= PolicyId and PolicyId<= %d"%(self.min,self.max)
        cursor = con.cursor()
        try:
            cursor.execute(sql_policy)
            rs = cursor.fetchall()
            for row in rs:
                list_row = list(row)
                list_row[3] = list_row[3][:150] + '....'
                #print(list_row[3])
                self.policy_all.append(list_row)
        except Exception as e:
            print(e)
            con.rollback()
        cursor.close()
        con.close()

    # 连接Policy数据库
    def policy_sql_id(self,id):
        list_data = []
        con = self.sql_content.conten()
        sql_policy = "select * from policy WHERE PolicyId = %d" % (id)
        cursor = con.cursor()
        try:
            cursor.execute(sql_policy)
            rs = cursor.fetchone()

            list_data = list(rs)
            list_data[4] = self.dict_turn[list_data[4]]
            print(list_data)
        except Exception as e:
            print(e)
            con.rollback()
        cursor.close()
        con.close()

        return list_data

    #检查数据库有几种类型type
    def policy_sql_type(self):
        list_data = []
        con = self.sql_content.conten()
        sql_policy = "select Type from policy"
        cursor = con.cursor()
        try:
            cursor.execute(sql_policy)
            rs = cursor.fetchall()
            for r in rs:
               if r not in list_data:
                   list_data.append(r)
            print(list_data)
        except Exception as e:
            print(e)
            con.rollback()
        cursor.close()
        con.close()
    #调用口
    def name_main(self):

        Policy = _Policy(self.min,self.max).policy_main()
        print('policy_class:',Policy)
        print('policy_class_1456:', self.policy_all)
        return Policy
#测试口
if __name__ == "__main__":
    _Policy(1,10).name_main()